package com.edu.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.edu.bean.User;
import com.edu.dao.UserDao;
import com.edu.dao.impl.UserDaoImpl;
import com.edu.service.UserService;

public class UserServiceImpl implements UserService {

	private UserDao userDao = new UserDaoImpl();
	
	@Override
	public List<User> getAllUser() {
		
		return userDao.selectAll();
	}

	@Override
	public Map<String,Object> userLogin(String username, String password) {
		Map<String,Object> map = new HashMap();
		User loginUser = userDao.selectUser(username, password);
		
		if(loginUser != null) {
			map.put("loginUser",loginUser);
		}else {
			loginUser = userDao.selectUserByUsername(username);
			if(loginUser == null) {
				map.put("msg","用户名不存在");
			}else {
				map.put("msg","密码错误");
			}
		}
		return map;
	}

	
	@Override
	public int userRegister(User user) {
		if(userDao.selectUserByUsername(user.getUsername())!=null) {
			return -1;
		}
		return userDao.insertUser(user);
	}

	@Override
	public int updateUser(String username, String oldPwd, String newPwd) {
		User user = userDao.selectUser(username, oldPwd);
		if(user == null) {
			return -1;
		}
		return userDao.updatePassword(username, newPwd);
	}

}
